import O1HD, {managers, modules, services} from "../../engines/gameEngine";
import {REWARD_TYPE} from "../../engines/services/userContent/userInterface";
import data_control from "../../engines/libs/data_control";
import gameConfig from "../../engines/configs/gameConfig";
import videoBtn from "../../Game/Obj/videoBtn";
import mainRoot from "../../Game/MainRoot";
import { PAGE } from "../../Game/Enum";

const {ccclass, property} = cc._decorator;

@ccclass
export default class addUserProp extends cc.Component {
    @property(cc.Node)
    detail: cc.Node = null;
    @property(cc.Label)
    title: cc.Label = null;
    @property(cc.Label)
    protected showNum: cc.Label = null;
    @property(cc.Sprite)
    protected spIcon: cc.Sprite = null;
    @property([cc.SpriteFrame])
    protected spfIcons: cc.SpriteFrame[] = [];
    @property({type:cc.Sprite,displayName:"标题图片"})
    private titleImg : cc.Sprite = null;
    @property({type:cc.Sprite,displayName:"内容图片"})
    private messageImg : cc.Sprite = null;

    @property({type:cc.Node,tooltip:"无限体力"})
    infinityBtn:cc.Node = null;

    @property({type:cc.Label,tooltip:"无限体力次数文本"})
    lbInfinityNum:cc.Label = null;

    @property({type:cc.Node,tooltip:"提示节点"})
    hintNode:cc.Node = null;

    @property({type:videoBtn})
    videoBtn:videoBtn = null;


    private static lookedTimes: number = 0

    // LIFE-CYCLE CALLBACKS:
    protected onLoad() {
    }

    protected start(): void {
        this.initView();
        this.node.zIndex = cc.macro.MAX_ZINDEX;
    }


    setData(propType: REWARD_TYPE, propNumber: number) {
        this.m_propType = propType;
        this.m_propCount = propNumber;

        // this.m_propType = REWARD_TYPE.propType;
        // this.m_propCount = 1;
        // if(propType == REWARD_TYPE.POWER){
        //     O1HD.getModules().uiControlModule.showInfinityPower(gameConfig.CLICK_POWER_INFINITY_PROBABILITY, mainRoot.UIRoot);
        // }
    }

    private m_propType: REWARD_TYPE = null;
    private m_propCount: number = null;

    protected initView() {
        // if (this.m_propType == null) {
        //     console.error('#505131 添加用户道具错误');
        //     return;
        // }
        //出场动画
        modules().uiControlModule.showHideDialogAnimation(this.detail, true);
        //
        this.showNum.string = `+${data_control.getNumberFormat(this.m_propCount)}`;
        this.showNum.node.active = this.m_propType != 0;
        this.videoBtn.enabled = this.m_propType != 0;
        // this.showNum.string = `:${data_control.getNumberFormat(this.m_propCount)}`;
        this.spIcon.spriteFrame = this.spfIcons[this.m_propType];
        this.infinityBtn.active = this.m_propType !=0 && !O1HD.getServices().userSrv.getInfinityPower();
        this.UpdateLookLabel();
        this.title.string = ['获取免广告币', '获取体力', '获取钻石'][this.m_propType];
        // let imgUrl = [null,'GetPowerTitle',null][this.m_propType];
        // this.title.node.active = (imgUrl==null);
        // node_component.loadResourcesPicture(`Img/${imgUrl}`,this.titleImg);
    }

    protected onBtnClose() {
        managers().soundMgr.playSound('mainSound/anNiu').then();
        this.onClose();
    }

    onClose(){
        modules().uiControlModule.showHideDialogAnimation(this.detail, false, () => {
            this.node.destroy();
        });
    }

    protected onBtnGet() {
        managers().soundMgr.playSound('mainSound/anNiu').then();
        let lookString = '未定义获取道具说明';
        let lookOverFunction: () => void = null;
        let rewardType: REWARD_TYPE = null;
        lookOverFunction = () => {
            services().userSrv.addPowerOfVideo();
        }
        rewardType = REWARD_TYPE.COIN;
        // lookString = '获取体力';
        // let lookString = '未定义获取道具说明';
        // let lookOverFunction: () => void = null;
        // let rewardType: REWARD_TYPE = null;
        switch (this.m_propType) {
            case REWARD_TYPE.COIN: {
                lookOverFunction = () => {
                    services().userSrv.setCoins(this.m_propCount, true);
                }
                rewardType = REWARD_TYPE.COIN;
                lookString = '获取免广告币';
                break;
            }
            case REWARD_TYPE.POWER: {
                lookOverFunction = () => {
                    services().userSrv.addPowerOfVideo();
                }
                rewardType = REWARD_TYPE.POWER;
                lookString = '获取体力';
                break;
            }
            // case REWARD_TYPE.DIAMOND: {
            //     lookOverFunction = () => {
            //         console.warn('#841801 钻石未定义');
            //     }
            //     rewardType = REWARD_TYPE.DIAMOND;
            //     lookString = '获取钻石';
            //     break;
            // }
            default: {
                throw '#900224 未知定义道具';
            }
        }

        let videoFun = ()=>{
            cc.director.pause();
            services().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
                lookOverFunction();
                //
                if(this.m_propType == REWARD_TYPE.COIN && O1HD.getServices().userSrv.getExtraAd() != "1"){
                    // 打开额外的界面
                    mainRoot.UISystem.OpenPage(PAGE.EXTRA_AD_PAGE);
                }else{    
                    modules().uiControlModule.showGetRewarded(rewardType, this.m_propCount);
                }
                cc.director.resume()
            }, ()=>{cc.director.resume()
            }, ()=>{cc.director.resume()
            }, lookString);
        }

        //查看视频
        if(this.m_propType == REWARD_TYPE.COIN){
            videoFun();
        }else{
            if(O1HD.getServices().userSrv.getCoins() > 0){
                O1HD.getServices().userSrv.setCoins(-1,true);
                lookOverFunction();
                modules().uiControlModule.showGetRewarded(rewardType, this.m_propCount);
            }else{
                videoFun();
            }
        }
    }

    infinityStr:string[] = [
        "再看两次就好啦",
        "看最后一次，谢谢老板",
    ]

    OnLookInfinityBtn() {
        O1HD.getManagers().soundMgr.playSound('mainSound/anNiu').then();
        let getFun = ()=>{
            let str = this.infinityStr[addUserProp.lookedTimes];
            this.showHintLb(str);
            addUserProp.lookedTimes++
            this.UpdateLookLabel()
            if (addUserProp.lookedTimes == gameConfig.INFINTY_VIDEO_TIME) {
                //进入无限体力模式
                O1HD.getServices().userSrv.setPower(999, true, true);
                O1HD.getServices().userSrv.setInfinity(1);
                O1HD.getServices().userSrv.setInfinityTime();
                O1HD.getModules().uiControlModule.showGetRewarded(REWARD_TYPE.POWER, 999, '获得体力');
                this.onClose();
            }
        }

        if (addUserProp.lookedTimes < gameConfig.INFINTY_VIDEO_TIME) {
            if(O1HD.getServices().userSrv.getCoins() >0){
                O1HD.getServices().userSrv.setCoins(-1,true);
                getFun();
            }else{
                O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(() => {
                        getFun();
                    }, () => {
                    },
                    null,
                    '解锁无限体力'
                )
            }

        }
    }
    
    showHintLb(str:string){
        if(str!= null){
            let node:cc.Node = cc.instantiate(this.hintNode);
            let hintLb:cc.Label = node.getChildByName("label").getComponent(cc.Label);
            hintLb.string = str;
            node.active = true;
            node.parent = this.hintNode.parent;
            cc.tween(node).by(2,{y:100}).call(()=>{
                node.destroy();
            }).start();
        }
    }

    UpdateLookLabel() {
        if (this.lbInfinityNum) {
            this.lbInfinityNum.string = `(${addUserProp.lookedTimes.toString()}/${gameConfig.INFINTY_VIDEO_TIME})`;
        }
    }
}
